package com.cys.dm.rest.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cys.dm.dao.order.DmOrderDao;
import com.cys.dm.entity.order.DmOrder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @author blue_t
 * @date 2020-12-27 9:02
 */
@RestController
public class DmOrderService {
    @Resource
    private DmOrderDao dmOrderDao;

    @PostMapping(path = "/addOrder")
    public Integer addOrder(@RequestBody DmOrder dmOrder){
        return dmOrderDao.insert(dmOrder);
    }

    /**
     * 根据订单号删除订单
     * @param orderNo
     * @return
     */
    @PostMapping(path = "/delOrder/{orderNo}")
    public Integer delOrder(@PathVariable("orderNo")String orderNo){
        QueryWrapper<DmOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orderNo",orderNo);
        return dmOrderDao.delete(queryWrapper);
    }

    /**
     * 根据订单编号查订单
     * @param orderNo
     * @return
     */
    @PostMapping(path = "/findOrderByOrderNo/{orderNo}")
    public DmOrder findOrderByOrderNo(@PathVariable("orderNo")String orderNo){
        QueryWrapper<DmOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orderNo",orderNo);
        return dmOrderDao.selectOne(queryWrapper);
    }

    @PostMapping(path = "/updataOrderType")
    public Integer updataOrderType(@RequestParam("orderNo")String orderNo, @RequestParam("orderType")long orderType){
        QueryWrapper<DmOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orderNo",orderNo);
        DmOrder dmOrder = dmOrderDao.selectOne(queryWrapper);
        //设置订单
        dmOrder.setOrderType(orderType);
        //添加订单记录
        return dmOrderDao.updateById(dmOrder);
    }

    @PostMapping(path = "/findByTypeAndTimeAndKey")
    public List<DmOrder> findByTypeAndTimeAndKey(@RequestParam(value = "orderType",required=false)Long orderType,
                                                 @RequestParam("orderTime")Long orderTime,
                                                 @RequestParam("keyword")String keyword,
                                                 @RequestParam("userId")Long userId){

        QueryWrapper<DmOrder> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("userId",userId);

        //查询订单支付状态类型
        if (orderType != null ) {
            queryWrapper.eq("orderType",orderType);
        }

        //设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        // new Date()为获取当前系统时间
        String datanow = df.format(new Date());
        //判断是否选择时间
        if (orderTime != null){
            if (orderTime == 1){
                //一年内
                System.out.println(orderTime);
            }
            if (orderTime == 2){
                //三个月内
            }
        }

        //判断模糊查询
        if (keyword != null && keyword != ""){
            //判断是不是数字
            Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
            if(pattern.matcher(keyword).matches()){
                queryWrapper.like("orderNo",keyword);
            }else {
                queryWrapper.like("itemName",keyword);
            }
        }

        return dmOrderDao.selectList(queryWrapper);
    }
}
